package com.nttuan.mycalender;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.nttuan.service2.AlarmService;
import com.nttuan.adapter.MyPaperAdapter;
import com.nttuan.flows.PageChange;
import com.nttuan.util.Support;
import com.nttuan.util.TypefaceSupport;
import com.nttuan.view.CustomViewPaper;

import java.util.Calendar;
import java.util.Date;

public class MainActivity extends FragmentActivity implements View.OnClickListener, ViewPager.OnPageChangeListener,
        PopupMenu.OnMenuItemClickListener{
    public static final String NUMBER_OF_NOTIFICATION = "number";
    public static final String PACKET="data";

    private ImageButton iBtnAddNew;
    private ImageButton iBtnMore;
    private ImageButton iBtnNotification;
    private ImageButton iBtnViewBy;
    private TextView selectedDate;
    private TextView notification;
    private CustomViewPaper viewPaper;
    private MyPaperAdapter paperAdapter;
    public PageChange pageChangeCenter;
    public PageChange pageChangeFreeTime;

//    public static AlarmService alarmService;
//    private ServiceConnection connection;
    private Intent intent;
    private MyReceiver myReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TypefaceSupport.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Think.ttf");
        setContentView(R.layout.activity_main);

        initView();
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    private void initView(){
        //init view
        iBtnAddNew = (ImageButton)findViewById(R.id.iBtnAddNew);
        iBtnNotification = (ImageButton)findViewById(R.id.iBtnNotification);
        iBtnMore = (ImageButton)findViewById(R.id.iBtnMore);
        iBtnViewBy = (ImageButton)findViewById(R.id.iBtnViewBy);
        viewPaper =(CustomViewPaper)findViewById(R.id.viewPaper);
        selectedDate = (TextView)findViewById(R.id.textSelectedDate);
        notification = (TextView)findViewById(R.id.textNotification);
        //inti listener
        viewPaper.setOnPageChangeListener(this);
        iBtnMore.setOnClickListener(this);
        iBtnAddNew.setOnClickListener(this);
        iBtnNotification.setOnClickListener(this);
        iBtnViewBy.setOnClickListener(this);
        selectedDate.setOnClickListener(this);
        //view paper
        paperAdapter = new MyPaperAdapter(getSupportFragmentManager());
        viewPaper.setAdapter(paperAdapter);
        selectedDate.setText(Support.currentDate());
    }
    private void initReceiver(){
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(AlarmService.ACTION);
        registerReceiver(myReceiver, intentFilter);

    }


    @Override
    protected void onStart() {
        initReceiver();
        super.onStart();
    }

    @Override
    protected void onStop() {
        unregisterReceiver(myReceiver);
        super.onStop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iBtnMore:
                PopupMenu popupMenu = new PopupMenu(this,iBtnMore);
                popupMenu.getMenuInflater().inflate(R.menu.popup_main, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(MainActivity.this);
                popupMenu.show();
                break;
            case R.id.iBtnAddNew:
                Intent intent = new Intent(this, AddNewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(AddNewActivity.KEY_ACTION, AddNewActivity.KEY_ACTION_ADD);
                intent.putExtra(AddNewActivity.KEY_BUNDLE, bundle);
                startActivity(intent);
                break;
            case R.id.textSelectedDate:
                selectedDate();
                break;
            case R.id.iBtnNotification:
                showNotification(0);
                Intent intentNotifi = new Intent(this, NotificationActivity.class);
                startActivity(intentNotifi);
                break;
            case R.id.iBtnViewBy:
//                PopupMenu popupMenuView = new PopupMenu(this,iBtnViewBy);
//                popupMenuView.getMenuInflater().inflate(R.menu.popup_view_by, popupMenuView.getMenu());
//                popupMenuView.setOnMenuItemClickListener(MainActivity.this);
//                popupMenuView.show();
                break;
        }
    }
    private void selectedDate(){
        final Dialog dialog = new Dialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_date_picker_new, null);
        dialog.setContentView(view);
        final DatePicker datePicker = (DatePicker)view.findViewById(R.id.datePickerDialog);
        Button btnOk = (Button)view.findViewById(R.id.btnOkPikerDialog);
        Button btnCancel = (Button)view.findViewById(R.id.btnCancelPikerDialog);
        Button btnToday = (Button)view.findViewById(R.id.btnTodayPikerDialog);
        final Calendar calendar = Calendar.getInstance();
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                calendar.set(Calendar.MONTH, datePicker.getMonth());
                calendar.set(Calendar.YEAR, datePicker.getYear());
                selectedDate.setText(Support.displayDate(calendar.getTime()));
                if(viewPaper.getCurrentItem() == 0)
                    pageChangeCenter.changeDate(calendar.getTime());
                else
                    pageChangeFreeTime.changeDate(calendar.getTime());
            }
        });
        btnToday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                selectedDate.setText(Support.displayDate(calendar.getTime()));
                if(viewPaper.getCurrentItem() == 0)
                    pageChangeCenter.changeDate(calendar.getTime());
                else
                    pageChangeFreeTime.changeDate(calendar.getTime());
            }
        });
        dialog.show();
    }
    private void showNotification(int numberOfMessage){
        if(numberOfMessage == 0) {
            notification.setVisibility(View.GONE);
            notification.setText("0");
        }
        else {
            int curr = Integer.parseInt(notification.getText().toString());
            notification.setVisibility(View.VISIBLE);
            notification.setText((numberOfMessage+curr)+"");
        }
    }
    @Override
    public void onPageScrolled(int i, float v, int i2) {

    }

    @Override
    public void onPageSelected(int i) {
        Date date = Support.toShortDate(selectedDate.getText().toString());
        pageChangeCenter.selectedPage(i, date);
        pageChangeFreeTime.selectedPage(i, date);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    //    pageChangeCenter.selectedPage(0, Support.toDateTime(selectedDate.getText().toString(),true));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.popup_by_day:
                pageChangeCenter.viewBy(PageChange.BY_DAY);
                break;
            case R.id.popup_by_week:
            //    pageChangeCenter.viewBy(PageChange.BY_WEEK);
                break;
            case R.id.popup_setting:
                Intent intent1 = new Intent(MainActivity.this, RegisterActivity.class);
                Bundle bundle = new Bundle();
                bundle.putBoolean(RegisterActivity.KEY_UPDATE, true);
                intent1.putExtra(RegisterActivity.KEY_BUNDLE, bundle);
                startActivity(intent1);
                break;
            case R.id.popup_exit:
                finish();
                break;
        }
        return true;
    }

    private class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getBundleExtra(PACKET);
            if(bundle != null) {
                showNotification(bundle.getInt(NUMBER_OF_NOTIFICATION));
            }
        }
    }
}
